package com.power.algorithm;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: jiangnan
 * @time: 2021/6/18 11:09 上午
 */
public class Solution387 {

    public int firstUniqChar(String s) {
        char[] arr = s.toCharArray();
        Map<Character, Integer> map = new HashMap<>();

        for (char ch : arr) {
            map.put(ch, map.getOrDefault(ch, 0) + 1);
        }
        for (int i = 0; i < arr.length; i++) {
            if (map.get(arr[i]) == 1) {
                return i;
            }
        }
        return -1;
    }

    public int firstUniqChar2(String s) {
        char[] count = new char[26];
        char[] arr = s.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            count[arr[i] - 'a']++;
        }
        for (int i = 0; i < arr.length; i++) {
            if (count[arr[i] - 'a'] == 1) {
                return i;
            }
        }
        return -1;
    }


    public int firstUniqChar3(String s) {
        char [] arr = s.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            if(s.indexOf(arr[i]) == s.lastIndexOf(arr[i]))
                return i;
        }
        return -1;
    }




    public static void main(String[] args) {
        Solution387 solution387 = new Solution387();
        solution387.firstUniqChar3("looklter");
    }
}
